package com.rabbiter.ordering.controller;

import com.rabbiter.ordering.entity.DishCategory;
import com.rabbiter.ordering.entity.Result;
import com.rabbiter.ordering.service.DishCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/api/category")
public class DishCategoryController {
    @Autowired
    private DishCategoryService  dishcategoryService;

    @GetMapping("/list")
    public Result list() {
        List<DishCategory> list = dishcategoryService.listAll();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        DishCategory category = dishcategoryService.getById(id);
        return Result.success(category);
    }
    @GetMapping("/listbyname")
    public Result getByName(DishCategory category) {
        List<DishCategory>  categorylist = dishcategoryService.getByName(category);
        return Result.success(categorylist);
    }
    @PostMapping
    public Result save(@RequestBody DishCategory category) {
        boolean success = dishcategoryService.saveCategory(category);
        return success ? Result.success() : Result.error("添加失败");
    }

    @PutMapping
    public Result update(@RequestBody DishCategory category) {
        boolean success = dishcategoryService.updateCategory(category);
        return success ? Result.success() : Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean success = dishcategoryService.deleteCategory(id);
        return success ? Result.success() : Result.error("删除失败");
    }
}
